﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.AI.Node;
using Yoozoo.Gameplay.YGUICustom;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetEventNpcNav2Destination : LatentActionNodeBase
    {
        public override bool allowRoutineQueueing { get { return false; } }

        private AIEntity aiEntity;
        private Vector3 _destination;
        private bool _isNaving = false;
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>("streetNpcId");
            var p2 = node.AddValueInput<string>("pointName");
            var p3 = node.AddValueInput<bool>("isSprint");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value,p2.value,p3.value), f); });
        }

        private IEnumerator Invoke(int streetNpcId, string pointName,bool isSprint)
        {
            var bp = BlueprintUtils.GetBlueprintPoint(pointName);
            if (bp == null)
            {
                yield break;
            }
            Vector3 destination = bp.position;
            _destination = destination;
            LibertyAIUtils.OnResetDefaultStatus += OnResetDefaultStatus;
            var md = StreetEventManager.GetInstance("").GetMonster(streetNpcId);
            if (md!=null)
            {
                aiEntity = LibertyAIUtils.GetNpc(md.uniqueId);
                if (aiEntity != null)
                {
                    _isNaving = true;
                    aiEntity.seeker.SwitchSpeedStrategy(SpeedStrategyType.Curve);
                    aiEntity.data.defaultNpcStatus = NpcStatus.None;
                    aiEntity.data.npcStatus = NpcStatus.None;
                    float speed = 1;
                    aiEntity.data.isSprint = isSprint;
                    aiEntity.seeker.SetMaxSpeed(aiEntity.propertyDataConfig.move_speed * speed);
                    aiEntity.data.maxSpeed = speed;
                    aiEntity.viewer.animator.SetFloat("Speed", speed);
                    yield return new WaitForOneFrame();
                    
                    aiEntity.data.destination = destination;
                    aiEntity.seeker.SetDestination(destination);
                    yield return new WaitForOneFrame();
                    yield return new UnityEngine.WaitUntil(() =>
                    {
                        return aiEntity.seeker.ReachedEndOfPath();
                    });

                    speed = 0;
                    aiEntity.data.isSprint = false;
                    aiEntity.seeker.SetMaxSpeed(aiEntity.propertyDataConfig.move_speed * speed);
                    aiEntity.data.maxSpeed = speed;
                    aiEntity.viewer.animator.SetFloat("Speed", speed);
                    _isNaving = false;
                }
            }
            LibertyAIUtils.OnResetDefaultStatus -= OnResetDefaultStatus;
        }

        private void OnResetDefaultStatus(int uniqueId, NpcStatus npcStatus)
        {
            if (!_isNaving)
            {
                return;
            }
            
            if (aiEntity == null)
            {
                return;
            }
            
            if (aiEntity.data.uniqueId != uniqueId)
            {
                return;
            }
            
            if (npcStatus == NpcStatus.None)
            {
                float speed = 1;
                aiEntity.seeker.SetMaxSpeed(aiEntity.propertyDataConfig.move_speed * speed);
                aiEntity.data.maxSpeed = speed;
                aiEntity.data.destination = _destination;
                aiEntity.seeker.SetDestination(_destination);
                aiEntity.viewer.animator.SetFloat("Speed", speed);
            }
        }


        public override void OnGraphStoped()
        {
            LibertyAIUtils.OnResetDefaultStatus -= OnResetDefaultStatus;
            base.OnGraphStoped();
        }
    }
}